import {Component, OnInit} from '@angular/core';
import {environment} from '../environments/environment';
import {Token} from './common/token';
import {TokenService} from './common/token.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  accessToken: string;
  auth: any;

  constructor(
    private token: Token,
    private service: TokenService
  ) {
  }

  logout() {
    this.token.logout();
    window.location.href = environment.oauth + '/logout?redirect=' + encodeURIComponent('http://localhost:4200');
  }

  login() {
    window.location.href = environment.oauth + '/oauth/authorize?response_type=code&client_id=client' +
      '&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Flogin';
  }

  ngOnInit(): void {
    this.service.checkToken();
    this.service.checkAuth();
    this.service.token$.subscribe(res => {
      this.accessToken = res;
    });
    this.service.auth$.subscribe(res => {
      this.auth = res;
    });
  }
}
